package com.liy.framework.common.jwt;


import com.liy.framework.common.extvo.LoginInfo;
import com.liy.framework.common.utils.Tools;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class JWTHelper
{
    private static RsaKeyHelper rsaKeyHelper;

    public static String generateToken(final LoginInfo jwtInfo, final String priKeyPath, final int expire) throws Exception {
        final String compactJws = Jwts.builder().setSubject(jwtInfo.getAccount()).claim("userId", jwtInfo.getId()).claim("name", jwtInfo.getName()).claim("loginIp", jwtInfo.getLoginIp()).signWith(SignatureAlgorithm.RS256,JWTHelper.rsaKeyHelper.getPrivateKey(priKeyPath)).compact();
        return compactJws;
    }

    public static String generateToken(final LoginInfo jwtInfo, final byte[] priKey, final int expire) throws Exception {
        final String compactJws = Jwts.builder().setSubject(jwtInfo.getAccount()).claim("userId", jwtInfo.getId()).claim("account", jwtInfo.getAccount()).claim("name", jwtInfo.getName()).claim("loginIp", jwtInfo.getLoginIp()).claim("userType",jwtInfo.getUserType()).claim("stationNo", jwtInfo.getStationNo()).claim("stationName", jwtInfo.getStationName()).signWith(SignatureAlgorithm.RS256, JWTHelper.rsaKeyHelper.getPrivateKey(priKey)).compact();
        return compactJws;
    }

    public static Jws<Claims> parserToken(final String token, final String pubKeyPath) throws Exception {
        final Jws<Claims> claimsJws = Jwts.parser().setSigningKey(JWTHelper.rsaKeyHelper.getPublicKey(pubKeyPath)).parseClaimsJws(token);
        return claimsJws;
    }

    public static Jws<Claims> parserToken(final String token, final byte[] pubKey) throws Exception {
        final Jws<Claims> claimsJws = Jwts.parser().setSigningKey(JWTHelper.rsaKeyHelper.getPublicKey(pubKey)).parseClaimsJws(token);
        return claimsJws;
    }

    public static LoginInfo getInfoFromToken(final String token, final String pubKeyPath) throws Exception {
        final Jws<Claims> claimsJws = parserToken(token, pubKeyPath);
        final Claims body = claimsJws.getBody();
        return new LoginInfo(body.getSubject(), Tools.getStringValue(body.get("userId")), Tools.getStringValue(body.get("name")), Tools.getStringValue(body.get("loginIp")));
    }

    public static LoginInfo getInfoFromToken(final String token, final byte[] pubKey) throws Exception {
        final Jws<Claims> claimsJws = parserToken(token, pubKey);
        final Claims body = claimsJws.getBody();
        final LoginInfo loginInfo = new LoginInfo(body.getSubject(), Tools.getStringValue(body.get("userId")), Tools.getStringValue(body.get("name")), Tools.getStringValue(body.get("loginIp")));
        loginInfo.setStationNo(Tools.getStringValue(body.get("stationNo")));
        loginInfo.setStationName(Tools.getStringValue(body.get("stationName")));
        return loginInfo;
    }

    public static void main(final String[] args) {
        final String jk = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInVzZXJJZCI6IjkwMCIsImFjY291bnQiOiJhZG1pbiIsIm5hbWUiOiLnrqHnkIblkZgiLCJsb2dpbklwIjoiMTAuMTQ2LjM4LjE4OCIsInVzZXJUeXBlIjoiQ1pZIiwic3RhdGlvbk5vIjoiMjQxMDE3Iiwic3RhdGlvbk5hbWUiOiLlrp3lronnq5kifQ.BhA960tipDOuVgseGIfaXIAOe1RqvO_Zp7ZbYYvM8ahEgcPBpbArDM1RFdOBHoaE9YYS1j5DF-BgaMbveoahGhhOp6ivWF1ug5l4EjlFt6aSTjvzxHdH3KHDhqlkmh0E8NYhnyYP-GTRK_Bhxx2ksWNFjg1kqw-jp1T7SLRcrKY";
    }

    static {
        JWTHelper.rsaKeyHelper = new RsaKeyHelper();
    }
}
